package by.gravity.grsu.shedule.util;

import java.util.Date;
import java.text.DateFormatSymbols;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class CalendarHelper {

	private static final int SHOW_WEEK_COUNT = 2;

	public static Date[] getPossibleSelected() {
		List<Date> result = new ArrayList<Date>();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(getStartOfWeekDate());
		result.add(calendar.getTime());

		for (int i = 0; i < SHOW_WEEK_COUNT; i++) {
			calendar.add(Calendar.DAY_OF_MONTH, 7);
			result.add(calendar.getTime());

		}
		return result.toArray(new Date[] {});
	}

	public static String[] convertToFriendlyDate(Date[] dates) {
		List<String> result = new ArrayList<String>();
		Calendar calendar = Calendar.getInstance();

		for (int i = 0; i < dates.length; i++) {
			calendar.setTime(dates[i]);
			result.add(String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)) + " "
					+ getMonth(calendar.get(Calendar.MONTH)));
		}

		return result.toArray(new String[] {});

	}

	public static Date getStartOfWeekDate() {
		Calendar calendar = Calendar.getInstance();

		calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());

		return getSupportedDate(calendar.getTime());
	}

	private static Date getSupportedDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return new Date(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
				calendar.get(Calendar.DAY_OF_MONTH));
	}

	private static String getMonth(int month) {
		return new DateFormatSymbols().getMonths()[month];
	}

}
